﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Group_Project_Book_Store
{
    class clsInvoiceItems
    {
        /// <summary>
        /// get and set for list of items
        /// </summary>
        public List<clsItem> listOfItems { get; set; }
        /// <summary>
        /// constructor
        /// </summary>
        public clsInvoiceItems()
        {
            listOfItems = new List<clsItem>();
        }
        /// <summary>
        /// add book item
        /// </summary>
        /// <param name="book"></param>
        /// <returns></returns>
        public List<clsItem> AddItem(clsBook book)
        {
            try
            {
                var templist = new List<clsItem>();
                clsItem item = new clsItem { bookTitle = book.bookTitle, Quantity = book.quantity, bookCost = book.bookCost, lineTotal = book.bookCost.ToString(), book = book };
                listOfItems.Add(item);

                var groupedListOfItems = (from items in listOfItems group items by items.bookTitle into newGroup select newGroup);

                foreach (var itemGroup in groupedListOfItems)
                {

                    clsItem invoice = new clsItem();

                    invoice.bookTitle = itemGroup.First().bookTitle;
                    invoice.bookCost = itemGroup.First().bookCost;
                    invoice.Quantity = itemGroup.ToList().Count();
                    invoice.lineTotal = (invoice.bookCost * invoice.Quantity).ToString();
                    invoice.book = itemGroup.First().book;

                    templist.Add(invoice);
                }
                return templist;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
                return null;
            }
        }
        public List<clsItem> RemoveItem(clsItem book)
        {
            try
            {
                var templist = new List<clsItem>();
                clsItem item = listOfItems.Where(b => b.book.bookID == book.book.bookID).First();
                listOfItems.Remove(item);

                var groupedListOfItems = (from items in listOfItems group items by items.bookTitle into newGroup select newGroup);

                foreach (var itemGroup in groupedListOfItems)
                {

                    clsItem invoice = new clsItem();

                    invoice.bookTitle = itemGroup.First().bookTitle;
                    invoice.bookCost = itemGroup.First().bookCost;
                    invoice.Quantity = itemGroup.ToList().Count();
                    invoice.lineTotal = (invoice.bookCost * invoice.Quantity).ToString();
                    invoice.book = itemGroup.First().book;

                    templist.Add(invoice);
                }
                return templist;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }
    } 
}
